package com.liao.tensquare.admin.service.impl;

import com.liao.tensquare.admin.dao.RoleDao;
import com.liao.tensquare.admin.pojo.TbRole;
import com.liao.tensquare.admin.service.ExcelService;
import com.liao.tensquare.admin.service.PermissionService;
import com.liao.tensquare.admin.service.TbRoleService;
import com.liao.tensquare.utils.DateUtils;
import com.liao.tensquare.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ExcelServiceImpl implements ExcelService {
    @Autowired
    private TbRoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RoleDao roleDao;

    @Override
    public void downExcelByRole(HttpServletResponse response, ArrayList<Integer> roleIds) {
//        List<Role> roles = roleService.findBatchByRoleIds(roleIds);
        List<TbRole> tbRoles = roleDao.selectBatchIds(roleIds);
        //List<String> roleColumnName = roleService.findColumnName();
        List<String> columnName = Arrays.asList(ExcelUtil.ROLE_COLUMN);
        List<List<String>> lists = getRoleList(tbRoles);
        ExcelUtil.uploadExcelAboutUser(response, "角色表", columnName, lists);
    }

    @Override
    public void downExcelByRoleAll(HttpServletResponse response) {
        List<TbRole> tbRoles = roleDao.selectList(null);
        //List<String> roleColumnName = roleService.findColumnName();
        List<String> columnName = Arrays.asList(ExcelUtil.ROLE_COLUMN);
        List<List<String>> lists = getRoleList(tbRoles);
        ExcelUtil.uploadExcelAboutUser(response, "角色表", columnName, lists);
    }

    @Override
    public void saveExcelByRole(InputStream inputStream) {
        List<List<String>> lists = ExcelUtil.readExcel(inputStream);
        System.out.println("lists=" + lists.toString());
    }

    private List<List<String>> getRoleList(List<TbRole> tbRoles) {
        List<List<String>> lists = new ArrayList<>();
        for (TbRole role : tbRoles) {
            List<String> list = new ArrayList<>();
            list.add(String.valueOf(role.getId()));
            list.add(role.getName());
            list.add(role.getDes());
            list.add(DateUtils.format(DateUtils.FormatType.type2, role.getCreateTime()));
            list.add(DateUtils.format(DateUtils.FormatType.type2, role.getUpdateTime()));
            if (String.valueOf(role.getIsDisable()).equals("0")) {
                list.add("启用");
            } else {
                list.add("停用");
            }
            lists.add(list);
        }
        return lists;
    }
}
